﻿using Sx.Mall.Domain;
using Sx.Mall.WebContract.Dto;
using Mall.Infrastructure.Model;
using Mall.Infrastructure.Repository;
using Mall.Infrastructure.EnityFramework;
using Sx.Mall.WebService.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sx.Mall.WebContract;
using System.Linq.Expressions;

namespace Sx.Mall.WebService
{
    public class ProductBigtypeService : IProductBigtypeService
    {
        private readonly AppDomain app = AppDomain.CurrentDomain;

        /// <summary>
        /// 添加产品大分类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public CommonResult Add(ProductBigtypeDto dto)
        {
            CommonResult result = new CommonResult();
            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var Repository = ctx.GetRepository<ProductBigtype>();
                var entity = dto.Convert<ProductBigtypeDto, ProductBigtype>();
                result = ProductBigtype.Add(Repository, entity);
                ctx.Commit();
            }
            return result;
        }

        /// <summary>
        /// 修改产品大分类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public CommonResult Update(ProductBigtypeDto dto)
        {
            CommonResult result = new CommonResult();
            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var Repository = ctx.GetRepository<ProductBigtype>();
                var entity = dto.Convert<ProductBigtypeDto, ProductBigtype>();
                result = ProductBigtype.Update(Repository, entity);
                ctx.Commit();
            }
            return result;
        }

        /// <summary>
        /// 删除产品大分类
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public CommonResult DeleteUser(CommonOperate<List<int>> option)
        {
            CommonResult result = new CommonResult();
            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var userRepository = ctx.GetRepository<ProductBigtype>();
                var smallRepository = ctx.GetRepository<ProductSmallType>();
                result = ProductBigtype.Delete(userRepository, smallRepository, option);
                ctx.Commit();
            }
            return result;
        }



        /// <summary>
        /// 查询产品大分类
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public PageResult<ProductBigtypeDto> GetList(ProductBigtypeDto dto)
        {
            Page page = dto.Page;
            PageResult<ProductBigtypeDto> result = new PageResult<ProductBigtypeDto>();
            Expression<Func<ProductBigtype, bool>> find = m => true && m.IsDelete==false;

            if (!string.IsNullOrWhiteSpace(dto.Name))
            {
                //根据大分类名称模糊查找
                find = find.And(m => m.Name.Contains(dto.Name));
            }

            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var userRepository = ctx.GetRepository<ProductBigtype>();

                var item = (from a in userRepository.Where(find)
                            select new ProductBigtypeDto
                            {
                                bigtypeId = a.bigtypeId,
                                CategoryCode=a.CategoryCode,
                                CreateID=a.CreateID,
                                CreateName=a.CreateName,
                                CreateTime=a.CreateTime,
                                Name=a.Name,
                                IsDelete=a.IsDelete,
                                Status = a.Status,

                            });
                result.rows = item.OrderBy(m => m.bigtypeId).Paging(page).ToList();
                result.total = page.Count;
            }
            return result;
        }

    }
}
